package com.example.administrator.jinritoutiao.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.jinritoutiao.R;
import com.example.administrator.jinritoutiao.fragment.home.HomeFragment1;
import com.example.administrator.jinritoutiao.fragment.home.HomeFragment2;
import com.example.administrator.jinritoutiao.fragment.home.HomeFragment3;
import com.example.administrator.jinritoutiao.fragment.home.HomeFragment4;
import com.example.administrator.jinritoutiao.fragment.home.HomeFragment5;
import com.example.administrator.jinritoutiao.fragment.home.HomeFragment6;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

public class HomeActivity extends FragmentActivity implements View.OnClickListener, ViewPager.OnPageChangeListener, RadioGroup.OnCheckedChangeListener {

    private RadioButton tv_tuijian ,tv_redian,tv_shipin,tv_bendi,tv_shehui,tv_dingyue;
    private ImageView iv_head,iv_find,iv_sj;
    private ViewPager vp_home;
    private SlidingMenu menu;
    private RadioGroup rg_home;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        menu = new SlidingMenu(getApplicationContext());
        menu.setMode(SlidingMenu.LEFT);
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        menu.attachToActivity(this,SlidingMenu.LEFT);
        menu.setBehindOffset(200);
        menu.setMenu(R.layout.sliding_menu);

        initView();
        initData();

        vp_home.setOnPageChangeListener(this);
        rg_home.setOnCheckedChangeListener(this);
        iv_head.setOnClickListener(this);
        iv_sj.setOnClickListener(this);
    }

    private void initData() {
        tv_tuijian.setChecked(true);
        for (int i = 0; i < rg_home.getChildCount(); i++) {
            RadioButton bt1 = (RadioButton) rg_home.getChildAt(i);
            if(bt1.isChecked()){
                bt1.setTextColor(Color.BLUE);
            }else{
                bt1.setTextColor(Color.BLACK);
            }
        }

        vp_home.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                Fragment fragment = null;
                switch (position){
                    case 0:
                        fragment = new HomeFragment1();
                        break;
                    case 1:
                        fragment = new HomeFragment2();
                        break;
                    case 2:
                        fragment = new HomeFragment3();
                        break;
                    case 3:
                        fragment = new HomeFragment4();
                        break;
                    case 4:
                        fragment = new HomeFragment5();
                        break;
                    case 5:
                        fragment = new HomeFragment6();
                        break;
                }
                return fragment;
            }

            @Override
            public int getCount() {
                return 6;
            }
        });

    }

    private void initView() {
        iv_head = (ImageView) findViewById(R.id.iv_head);
        iv_find = (ImageView) findViewById(R.id.iv_find);
        iv_sj = (ImageView) findViewById(R.id.iv_sj);
        rg_home = (RadioGroup) findViewById(R.id.rg_home);
        tv_tuijian = (RadioButton) findViewById(R.id.tv_tuijian);
        tv_redian = (RadioButton) findViewById(R.id.tv_redian);
        tv_shipin = (RadioButton) findViewById(R.id.tv_shipin);
        tv_bendi = (RadioButton) findViewById(R.id.tv_bendi);
        tv_shehui = (RadioButton) findViewById(R.id.tv_shehui);
        tv_dingyue = (RadioButton) findViewById(R.id.tv_dingyue);
        vp_home = (ViewPager) findViewById(R.id.vp_home);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_head:
                menu.toggle();
                break;
            case R.id.iv_sj:
                Intent intent = new Intent(this,ChannelActivity.class);
                startActivity(intent);
//                overridePendingTransition(R.anim.up_in,R.anim.down_out);
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if(position==0){
            menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        }else{
            menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
        }
        RadioButton bt = (RadioButton) rg_home.getChildAt(position);
             bt.setChecked(true);
        for (int i = 0; i < rg_home.getChildCount(); i++) {
            RadioButton bt1 = (RadioButton) rg_home.getChildAt(i);
            if(bt1.isChecked()){
                bt1.setTextColor(Color.BLUE);
            }else{
                bt1.setTextColor(Color.BLACK);

            }
        }

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId){
            case R.id.tv_tuijian:
                vp_home.setCurrentItem(0);
                break;
            case R.id.tv_redian:
                vp_home.setCurrentItem(1);
                break;
            case R.id.tv_shipin:
                vp_home.setCurrentItem(2);
                break;
            case R.id.tv_bendi:
                vp_home.setCurrentItem(3);
                break;
            case R.id.tv_shehui:
                vp_home.setCurrentItem(4);
                break;
            case R.id.tv_dingyue:
                vp_home.setCurrentItem(5);
                break;
        }
    }
}
